// 地址配置
// const baseUrl = 'http://192.168.1.61';

// 图片上传地址
// const uploadImage = 'http://192.168.1.61';

// const baseUrl = 'https://559.bjsaiya.cn';
// const uploadImage = 'https://559.bjsaiya.cn';

//川东服务器
// const baseUrl = 'http://119.4.40.169:16888';
// const uploadImage = 'http://119.4.40.169:16888';
// const baseUrl = 'http://7559854r7k.goho.co:57569'; //fl测试打包
// const uploadImage = 'http://7559854r7k.goho.co:57569';
const baseUrl = "http://119.4.40.169:16888";
// const baseUrl = "http://10.22.62.49:16888";
// const baseUrl = 'http://119.4.40.169:16888';
const uploadImage = "http://lz559"; //php兄弟的图片
// const baseUrl = 'http://192.168.1.26:8001';//测试用6.20
// const uploadImage = 'http://192.168.1.26:8001';
// const baseUrl = 'http://559.cxbxczx.com';//php兄弟的图片
// const uploadImage = 'http://559.cxbxczx.com';//php兄弟的图片
// const baseUrl = 'http://7559854r7k.goho.co';
// const uploadImage = 'http://7559854r7k.goho.co';
// const uploadImage = 'http://559.dev';
// const baseUrl = 'http://559.dev';
// const uploadImage = 'http://192.168.1.26:8001';
/*
 *公共请求方法
 *config.type: 请求类型  post|get  默认post
 *config.url: 请求地址
 *config.data: 请求参数
 *config.debug: 调试打印数据
 *config.title: 调试显示标题
 *config.success: 成功回调
 *config.fail：失败回调
 */
const request = (config) => {
  let headers = {
    "Content-Type": "application/json;charset=utf-8",
    Authorization: "Bearer " + uni.getStorageSync("TOKEN"),
  };

  uni.request({
    url: `${config.baseUrl || baseUrl}${config.url}`,
    method: config.type ? config.type : "post",
    header: headers,
    data: {
      // token: uni.getStorageSync("TOKEN"),
      ...config.data,
    },
    success: (res) => {
      if (res.data.code == 500) {
        uni.showToast({
          title: res.data.msg,
          icon: "none",
        });
        return;
      }
      if (res.data.code == 200) {
        if (config.debug) {
        }
        config.success(res.data.data);
      } else if (res.data.code == 403) {
				uni.setStorageSync('TOKEN', '');
        if (config.debug) {
          uni.redirectTo({
            url: "../auth/index",
          });
        }
        uni.showToast({
          title: res.data.msg,
          icon: "none",
        });
        config.fail();
        return;
      } else if (res.data.code == 401) {
        uni.showToast({
          title: res.data.msg,
          icon: "none",
        });
        config.fail();
        return;
      } else {
        return;
      }
    },
    fail: (res) => {
      config.fail();
      if (config.debug) {
        uni.showToast({
          title: "请求失败,请查看控制台",
          icon: "none",
        });
      } else {
        uni.showToast({
          title: "请求失败",
          icon: "none",
        });
      }
    },
  });
};
/*
 * 单文件上传
 *
 **/
export const uploadFile = (params, success, fail) => {
  let headers = {
    "Content-Type": "application/json;charset=utf-8",
    Authorization: "Bearer " + uni.getStorageSync("TOKEN"),
  };
  let newL = [];
  if (params.length <= 0) {
    success(newL);
    return;
  }
  for (let i = 0; i < params.length; i++) {
    // 数据请求
    uni.uploadFile({
      url: uploadImage + "/api/v1/upload",
      filePath: params[i],
      name: "file",
      // header: config.header,
      // formData: config.formData,
      // timeout: config.timeout,
      success: (res) => {
        if (res.statusCode == 200) {
          let str = JSON.parse(res.data);
          newL.push(str.data.url);
          if (i == params.length - 1) {
            success(newL);
          }
        }
      },
      fail: (res) => {
        fail();
      },
    });
  }
};

// 用户登录 外部
export const postLogin = (data, debug, success, fail) => {
  request({
    url: "/api/v1/login",
    type: "post",
    data: data,
    debug: debug,
    title: "用户登录",
    success: success,
    fail: fail,
  });
};
// 用户登录 外部code
export const codeLogin = (data, debug, success, fail) => {
  request({
    url: "/api/oauth/jst/login",
    type: "post",
    data: data,
    debug: debug,
    title: "用户登录",
    success: success,
    fail: fail,
  });
};

// 内部员工登录 
export const normalLogin = (data, debug, success, fail) => {
  request({
    url: "/api/oauth/normal/login",
    type: "post",
    data: data,
    debug: debug,
    title: "用户登录",
    success: success,
    fail: fail,
  });
};

// 获取用户当前考勤
export const getAttendance = (data, debug, success, fail) => {
  request({
    url: "/api/v1/attendance/dayList",
    type: "get",
    data: data,
    debug: debug,
    title: "获取用户当前考勤",
    success: success,
    fail: fail,
  });
};
// 更换头像
export const changeHead = (data, debug, success, fail) => {
  request({
    url: "/api/v1/user/updateImg",
    type: "PUT",
    data: data,
    debug: debug,
    title: "更换头像",
    success: success,
    fail: fail,
  });
};
// 获取考勤班次  /api/v1/attendance/attendanceScheduleList
export const getAttendanceScheduleList = (data, debug, success, fail) => {
  request({
    url: "/api/v1/attendance/attendanceScheduleList",
    type: "get",
    data: data,
    debug: debug,
    title: "获取考勤班次",
    success: success,
    fail: fail,
  });
};

// 获取考勤状态
export const getAttendanceState = (data, debug, success, fail) => {
  request({
    url: "/api/v1/attendance/groupStatus",
    type: "get",
    data: data,
    debug: debug,
    title: "获取考勤状态",
    success: success,
    fail: fail,
  });
};

// 获取消息列表
export const getMessages = (data, debug, success, fail) => {
  request({
    url: "/api/v1/user/message",
    type: "get",
    data: data,
    debug: debug,
    title: "获取消息列表",
    success: success,
    fail: fail,
  });
};

// 获取公告详情
export const getMessagesDetail = (data, debug, success, fail) => {
  request({
    url: "/api/v1/user/noticeDetail",
    type: "get",
    data: data,
    debug: debug,
    title: "获取公告详情",
    success: success,
    fail: fail,
  });
};

// 获取用户信息
export const getUserInfo = (data, debug, success, fail) => {
  request({
    url: "/api/v1/user/info",
    type: "get",
    data: data,
    debug: debug,
    title: "获取用户信息",
    success: success,
    fail: fail,
  });
};

// 获取考勤组
export const getAttGroup = (data, debug, success, fail) => {
  request({
    url: "/api/v1/attendance/group",
    type: "get",
    data: data,
    debug: debug,
    title: "获取用户信息",
    success: success,
    fail: fail,
  });
};

// 上报考勤
export const postReport = (data, debug, success, fail) => {
  request({
    url: "/api/v1/attendance/apply",
    type: "post",
    data: data,
    debug: debug,
    title: "上报考勤",
    success: success,
    fail: fail,
  });
};

// 考勤打卡
export const postPunchClock = (data, debug, success, fail) => {
  request({
    url: "/api/v1/attendance/punch",
    type: "post",
    data: data,
    debug: debug,
    title: "考勤打卡",
    success: success,
    fail: fail,
  });
};

// 获取申请类型
export const getApplyType = (data, debug, success, fail) => {
  request({
    url: "/api/v1/workflow/type",
    type: "get",
    data: data,
    debug: debug,
    title: "获取申请类型",
    success: success,
    fail: fail,
  });
};

// 获取申请流程
export const getProcess = (data, debug, success, fail) => {
  request({
    url: "/api/v1/workflow/flowProcess",
    type: "get",
    data: data,
    debug: debug,
    title: "获取申请流程",
    success: success,
    fail: fail,
  });
};

// 获取请假工时
export const getDuration = (data, debug, success, fail) => {
  request({
    url: "/api/v1/workflow/computingTime",
    type: "get",
    data: data,
    debug: debug,
    title: "获取请假工时",
    success: success,
    fail: fail,
  });
};

// 获取可缺卡记录
export const getMakeupCard = (data, debug, success, fail) => {
  request({
    url: "/api/v1/workflow/getMissingCard",
    type: "get",
    data: data,
    debug: debug,
    title: "获取可缺卡记录",
    success: success,
    fail: fail,
  });
};

// 申请提交
export const postSbumitInfo = (data, debug, success, fail) => {
  request({
    url: "/api/v1/workflow/applyFlow",
    type: "post",
    data: data,
    debug: debug,
    title: "申请提交",
    success: success,
    fail: fail,
  });
};

// 获取申请列表
export const getApplyList = (data, debug, success, fail) => {
  request({
    url: "/api/v1/workflow/applyFlowList",
    type: "get",
    data: data,
    debug: debug,
    title: "获取申请列表",
    success: success,
    fail: fail,
  });
};

// 获取审核列表
export const getApproveList = (data, debug, success, fail) => {
  request({
    url: "/api/v1/workflow/auditFlow",
    type: "get",
    data: data,
    debug: debug,
    title: "获取审核列表",
    success: success,
    fail: fail,
  });
};

// 撤销申请
export const revokeApply = (data, debug, success, fail) => {
  request({
    url: "/api/v1/workflow/cancelApply",
    type: "post",
    data: data,
    debug: debug,
    title: "撤销申请",
    success: success,
    fail: fail,
  });
};

// 获取申请详情
export const getApplyInfo = (data, debug, success, fail) => {
  request({
    url: "/api/v1/workflow/applyFlowDetail",
    type: "get",
    data: data,
    debug: debug,
    title: "获取申请详情",
    success: success,
    fail: fail,
  });
};

// 审核控制
export const postApproveSubmit = (data, debug, success, fail) => {
  request({
    url: "/api/v1/workflow/auditFlowStatus",
    type: "post",
    data: data,
    debug: debug,
    title: "审核控制",
    success: success,
    fail: fail,
  });
};

// 获取考勤权限
export const postAttendance = (data, debug, success, fail) => {
  request({
    url: "/api/v1/attendance/auth",
    type: "get",
    data: data,
    debug: debug,
    title: "获取考勤权限",
    success: success,
    fail: fail,
  });
};

//获取月汇总数据
export const getMonthAttend = (data, debug, success, fail) => {
  request({
    url: "/api/v1/attendance/month",
    type: "get",
    data: data,
    debug: debug,
    title: "获取月汇总数据",
    success: success,
    fail: fail,
  });
};

//获取天考勤数据数据
export const getDayAttendance = (data, debug, success, fail) => {
  request({
    url: "/api/v1/attendance/dayList",
    type: "get",
    data: data,
    debug: debug,
    title: "获取天考勤数据数据",
    success: success,
    fail: fail,
  });
};

//获取缺卡时间
export const getMissingCard = (data, debug, success, fail) => {
  request({
    url: "/api/v1/workflow/getMissingCard",
    type: "get",
    data: data,
    debug: debug,
    title: "获取缺卡时间",
    success: success,
    fail: fail,
  });
};

//获取销假时间
export const getTimeCard = (data, debug, success, fail) => {
  request({
    url: "/api/v1/workflow/getLeaveList",
    type: "get",
    data: data,
    debug: debug,
    title: "获取销假时间",
    success: success,
    fail: fail,
  });
};
// 获取同部门用户列表
export const getDeptUser = (data, debug, success, fail) => {
  request({
    url: "/api/v1/user/GetDeptUser",
    type: "get",
    data: data,
    debug: debug,
    title: "获取同部门用户列表",
    success: success,
    fail: fail,
  });
};

// 获取同部门用户列表
export const holiday = (data, debug, success, fail) => {
  request({
    url: "/api/v1/workflow/holiday",
    type: "post",
    data: data,
    debug: debug,
    title: "节假日预申请",
    success: success,
    fail: fail,
  });
};
// 获取同部门用户列表
export const addApplyFlow = (data, debug, success, fail) => {
  request({
    url: "/api/v1/workflow/applyFlow",
    type: "post",
    data: data,
    debug: debug,
    title: "节假日预申请",
    success: success,
    fail: fail,
  });
};
